import { defineStore } from 'pinia'
import {ref} from 'vue'
import type {Ref} from 'vue'

export const useUserStore = defineStore('user', ()=>{

  // 定义一个全局状态
  // 用来标记是否登录
  const loginState: Ref<boolean> = ref(localStorage.getItem('loginState') == 'true')

  function changeLoginState(value: boolean){
    // 修改全局状态中的数据
    loginState.value = value
  }

  // 用来记录登录之后用户的 id
  const userid: Ref<string> = ref(localStorage.getItem('userid') || '')
  function changeUserId( value: string){
    userid.value = value
  }

  // 将全局状态和方法暴露出去
  return {
    loginState,
    changeLoginState,
    userid,
    changeUserId
  }
})
